using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Material Nav Drawer", 100)]
	public class MaterialNavDrawer : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IEventSystemHandler
	{
		[SerializeField]
		private Image m_BackgroundImage;

		[SerializeField]
		private Image m_ShadowImage;

		[SerializeField]
		private GameObject m_PanelLayer;

		[SerializeField]
		private bool m_DarkenBackground = true;

		[SerializeField]
		private bool m_TapBackgroundToClose = true;

		[SerializeField]
		private bool m_OpenOnStart;

		[SerializeField]
		private float m_AnimationDuration = 0.5f;

		private MaterialUIScaler m_Scaler;

		private float m_MaxPosition;

		private float m_MinPosition;

		private RectTransform m_RectTransform;

		private GameObject m_BackgroundGameObject;

		private RectTransform m_BackgroundRectTransform;

		private CanvasGroup m_BackgroundCanvasGroup;

		private GameObject m_ShadowGameObject;

		private CanvasGroup m_ShadowCanvasGroup;

		private byte m_AnimState;

		private float m_AnimStartTime;

		private float m_AnimDeltaTime;

		private Vector2 m_CurrentPos;

		private float m_CurrentBackgroundAlpha;

		private float m_CurrentShadowAlpha;

		private Vector2 m_TempVector2;

		public Image backgroundImage
		{
			get
			{
				return m_BackgroundImage;
			}
			set
			{
				m_BackgroundImage = value;
			}
		}

		public Image shadowImage
		{
			get
			{
				return m_ShadowImage;
			}
			set
			{
				m_ShadowImage = value;
			}
		}

		public GameObject panelLayer
		{
			get
			{
				return m_PanelLayer;
			}
			set
			{
				m_PanelLayer = value;
			}
		}

		public bool darkenBackground
		{
			get
			{
				return m_DarkenBackground;
			}
			set
			{
				m_DarkenBackground = value;
			}
		}

		public bool tapBackgroundToClose
		{
			get
			{
				return m_TapBackgroundToClose;
			}
			set
			{
				m_TapBackgroundToClose = value;
			}
		}

		public bool openOnStart
		{
			get
			{
				return m_OpenOnStart;
			}
			set
			{
				m_OpenOnStart = value;
			}
		}

		public float animationDuration
		{
			get
			{
				return m_AnimationDuration;
			}
			set
			{
				m_AnimationDuration = value;
			}
		}

		public MaterialUIScaler scaler
		{
			get
			{
				if (m_Scaler == null)
				{
					m_Scaler = MaterialUIScaler.GetParentScaler(base.transform);
				}
				return m_Scaler;
			}
		}

		private void Awake()
		{
			m_RectTransform = base.gameObject.GetComponent<RectTransform>();
			m_BackgroundRectTransform = m_BackgroundImage.GetComponent<RectTransform>();
			m_BackgroundCanvasGroup = m_BackgroundImage.GetComponent<CanvasGroup>();
			m_ShadowCanvasGroup = m_ShadowImage.GetComponent<CanvasGroup>();
		}

		private void Start()
		{
			m_MaxPosition = m_RectTransform.rect.width / 2f;
			m_MinPosition = 0f - m_MaxPosition;
			RefreshBackgroundSize();
			m_BackgroundGameObject = m_BackgroundImage.gameObject;
			m_ShadowGameObject = m_ShadowImage.gameObject;
			if (m_OpenOnStart)
			{
				Open();
				return;
			}
			m_BackgroundGameObject.SetActive(value: false);
			m_ShadowGameObject.SetActive(value: false);
			m_PanelLayer.SetActive(value: false);
		}

		public void BackgroundTap()
		{
			if (m_TapBackgroundToClose)
			{
				Close();
			}
		}

		public void Open()
		{
			RefreshBackgroundSize();
			m_BackgroundGameObject.SetActive(value: true);
			m_ShadowGameObject.SetActive(value: true);
			m_PanelLayer.SetActive(value: true);
			m_CurrentPos = m_RectTransform.anchoredPosition;
			m_CurrentBackgroundAlpha = m_BackgroundCanvasGroup.alpha;
			m_CurrentShadowAlpha = m_ShadowCanvasGroup.alpha;
			m_BackgroundCanvasGroup.blocksRaycasts = true;
			m_AnimStartTime = Time.realtimeSinceStartup;
			m_AnimState = 1;
		}

		public void Close()
		{
			m_CurrentPos = m_RectTransform.anchoredPosition;
			m_CurrentBackgroundAlpha = m_BackgroundCanvasGroup.alpha;
			m_CurrentShadowAlpha = m_ShadowCanvasGroup.alpha;
			m_BackgroundCanvasGroup.blocksRaycasts = false;
			m_AnimStartTime = Time.realtimeSinceStartup;
			m_AnimState = 2;
		}

		private void RefreshBackgroundSize()
		{
			RectTransform backgroundRectTransform = m_BackgroundRectTransform;
			float x = (float)Screen.width / scaler.scaler.scaleFactor + 1f;
			Vector2 sizeDelta = m_BackgroundRectTransform.sizeDelta;
			backgroundRectTransform.sizeDelta = new Vector2(x, sizeDelta.y);
		}

		private void Update()
		{
			if (m_AnimState == 1)
			{
				m_AnimDeltaTime = Time.realtimeSinceStartup - m_AnimStartTime;
				if (m_AnimDeltaTime <= m_AnimationDuration)
				{
					RectTransform rectTransform = m_RectTransform;
					Vector2 currentPos = m_CurrentPos;
					float maxPosition = m_MaxPosition;
					Vector2 anchoredPosition = m_RectTransform.anchoredPosition;
					rectTransform.anchoredPosition = Tween.QuintOut(currentPos, new Vector2(maxPosition, anchoredPosition.y), m_AnimDeltaTime, m_AnimationDuration);
					if (m_DarkenBackground)
					{
						m_BackgroundCanvasGroup.alpha = Tween.QuintOut(m_CurrentBackgroundAlpha, 1f, m_AnimDeltaTime, m_AnimationDuration);
					}
					m_ShadowCanvasGroup.alpha = Tween.QuintIn(m_CurrentShadowAlpha, 1f, m_AnimDeltaTime, m_AnimationDuration / 2f);
				}
				else
				{
					RectTransform rectTransform2 = m_RectTransform;
					float maxPosition2 = m_MaxPosition;
					Vector2 anchoredPosition2 = m_RectTransform.anchoredPosition;
					rectTransform2.anchoredPosition = new Vector2(maxPosition2, anchoredPosition2.y);
					if (m_DarkenBackground)
					{
						m_BackgroundCanvasGroup.alpha = 1f;
					}
					m_AnimState = 0;
				}
			}
			else if (m_AnimState == 2)
			{
				m_AnimDeltaTime = Time.realtimeSinceStartup - m_AnimStartTime;
				if (m_AnimDeltaTime <= m_AnimationDuration)
				{
					RectTransform rectTransform3 = m_RectTransform;
					Vector2 currentPos2 = m_CurrentPos;
					float minPosition = m_MinPosition;
					Vector2 anchoredPosition3 = m_RectTransform.anchoredPosition;
					rectTransform3.anchoredPosition = Tween.QuintOut(currentPos2, new Vector2(minPosition, anchoredPosition3.y), m_AnimDeltaTime, m_AnimationDuration);
					if (m_DarkenBackground)
					{
						m_BackgroundCanvasGroup.alpha = Tween.QuintOut(m_CurrentBackgroundAlpha, 0f, m_AnimDeltaTime, m_AnimationDuration);
					}
					m_ShadowCanvasGroup.alpha = Tween.QuintIn(m_CurrentShadowAlpha, 0f, m_AnimDeltaTime, m_AnimationDuration);
				}
				else
				{
					RectTransform rectTransform4 = m_RectTransform;
					float minPosition2 = m_MinPosition;
					Vector2 anchoredPosition4 = m_RectTransform.anchoredPosition;
					rectTransform4.anchoredPosition = new Vector2(minPosition2, anchoredPosition4.y);
					if (m_DarkenBackground)
					{
						m_BackgroundCanvasGroup.alpha = 0f;
					}
					m_BackgroundGameObject.SetActive(value: false);
					m_ShadowGameObject.SetActive(value: false);
					m_PanelLayer.SetActive(value: false);
					m_AnimState = 0;
				}
			}
			RectTransform rectTransform5 = m_RectTransform;
			Vector2 anchoredPosition5 = m_RectTransform.anchoredPosition;
			float x = Mathf.Clamp(anchoredPosition5.x, m_MinPosition, m_MaxPosition);
			Vector2 anchoredPosition6 = m_RectTransform.anchoredPosition;
			rectTransform5.anchoredPosition = new Vector2(x, anchoredPosition6.y);
		}

		public void OnBeginDrag(PointerEventData data)
		{
			RefreshBackgroundSize();
			m_AnimState = 0;
			m_BackgroundGameObject.SetActive(value: true);
			m_ShadowGameObject.SetActive(value: true);
			m_PanelLayer.SetActive(value: true);
		}

		public void OnDrag(PointerEventData data)
		{
			m_TempVector2 = m_RectTransform.anchoredPosition;
			ref Vector2 tempVector = ref m_TempVector2;
			float x = tempVector.x;
			Vector2 delta = data.delta;
			tempVector.x = x + delta.x / scaler.scaler.scaleFactor;
			m_RectTransform.anchoredPosition = m_TempVector2;
			if (m_DarkenBackground)
			{
				CanvasGroup backgroundCanvasGroup = m_BackgroundCanvasGroup;
				float maxPosition = m_MaxPosition;
				Vector2 anchoredPosition = m_RectTransform.anchoredPosition;
				backgroundCanvasGroup.alpha = 1f - (maxPosition - anchoredPosition.x) / (m_MaxPosition - m_MinPosition);
			}
			CanvasGroup shadowCanvasGroup = m_ShadowCanvasGroup;
			float maxPosition2 = m_MaxPosition;
			Vector2 anchoredPosition2 = m_RectTransform.anchoredPosition;
			shadowCanvasGroup.alpha = 1f - (maxPosition2 - anchoredPosition2.x) / ((m_MaxPosition - m_MinPosition) * 2f);
		}

		public void OnEndDrag(PointerEventData data)
		{
			Vector2 delta = data.delta;
			if (Mathf.Abs(delta.x) >= 0.5f)
			{
				Vector2 delta2 = data.delta;
				if (delta2.x > 0.5f)
				{
					Open();
				}
				else
				{
					Close();
				}
				return;
			}
			Vector2 anchoredPosition = m_RectTransform.anchoredPosition;
			float num = anchoredPosition.x - m_MinPosition;
			float maxPosition = m_MaxPosition;
			Vector2 anchoredPosition2 = m_RectTransform.anchoredPosition;
			if (num > maxPosition - anchoredPosition2.x)
			{
				Open();
			}
			else
			{
				Close();
			}
		}
	}
}
